package com.zzyl.nursing.controller.member;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.zzyl.common.utils.UserThreadLocal;
import com.zzyl.nursing.dto.BindElderDto;
import com.zzyl.nursing.dto.UserLoginRequestDto;
import com.zzyl.nursing.vo.FamilyListVo;
import com.zzyl.nursing.vo.FamilyVo;
import com.zzyl.common.core.page.PageDomain;
import com.zzyl.nursing.domain.NursingProject;
import com.zzyl.nursing.dto.*;
import com.zzyl.nursing.vo.LoginVo;
import com.zzyl.nursing.vo.ReservationCountVo;
import com.zzyl.nursing.vo.ReservationPageVo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.zzyl.common.annotation.Log;
import com.zzyl.common.core.controller.BaseController;
import com.zzyl.common.core.domain.AjaxResult;
import com.zzyl.common.enums.BusinessType;
import com.zzyl.nursing.domain.FamilyMember;
import com.zzyl.nursing.service.IFamilyMemberService;
import com.zzyl.common.utils.poi.ExcelUtil;
import com.zzyl.common.core.page.TableDataInfo;

/**
 * 老人家属Controller
 * 
 * @author ruoyi
 * @date 2025-08-26
 */
@RestController
@RequestMapping("/member")
public class FamilyMemberController extends BaseController {
    @Autowired
    private IFamilyMemberService familyMemberService;


    @PostMapping("/user/login")
    public AjaxResult login(@RequestBody UserLoginRequestDto userLoginRequestDto) {
        LoginVo loginVo = familyMemberService.login(userLoginRequestDto);
        return success(loginVo);

    }

    @PostMapping("/user/add")
    public AjaxResult addfamily(@RequestBody BindElderDto bindElderDto) {
        //绑定家人
        Long familyMemberId = UserThreadLocal.getUserId();
        familyMemberService.addfamily(bindElderDto, familyMemberId);
        return success();
    }

    //查询家人列表
    @GetMapping("/user/my")
    public AjaxResult getMyFamilyList() {
        Long familyMemberId = UserThreadLocal.getUserId();
        List<FamilyListVo> list = familyMemberService.getMyFamilyList(familyMemberId);
        return success(list);
    }

    /**
     * 查询老人家属列表
     */
    @GetMapping("/orders/project/page")
    public TableDataInfo list(ProjectPageDto projectPageDto) {
        startPage();
        List<NursingProject> list = familyMemberService.selectNursingProjectList(projectPageDto);
        return getDataTable(list);
    }

    /**
     * 根据编号查询项目信息
     */
    @GetMapping("/orders/project/{id}")
    public AjaxResult info(@PathVariable("id") Long id) {
        NursingProject nursingProject = familyMemberService.selectNursingProjectById(id);
        return success(nursingProject);
    }

    /**
     * 查询取消预约数量
     */
    @GetMapping("/reservation/cancelled-count")
    public AjaxResult cancelledCount() {
        int count = familyMemberService.selectCancelledCount();
        // 返回JSON格式的响应
        Map<String, Object> result = new HashMap<>();
        result.put("count", count);
        return success(result);
    }

    /**
     * 查询每个时间段的剩余预约数
     */
    @GetMapping("/reservation/countByTime")
    public AjaxResult countByTime(@RequestParam(required = false) Long time) {
        List<ReservationCountVo> list = familyMemberService.selectReservationCountByTime(time);
        return AjaxResult.success(list);
    }

    /**
     * 查询家人列表
     */
    @GetMapping("/user/list-by-page")
    public AjaxResult listByPage() {
        Long familyMemberId = UserThreadLocal.getUserId();
        List<FamilyVo> list = familyMemberService.selectFamilyList(familyMemberId);
        return success(list);

    }

    /**
     * 新增预约
     */
    @PostMapping("/reservation")
    public AjaxResult add(@RequestBody ProjectAddDto projectAddDto) {
        familyMemberService.insertReservation(projectAddDto);
        return success();
    }


    /**
     * 取消绑定
     */
    @DeleteMapping("/user/deleteById")
    public AjaxResult deleteById(@RequestParam Long id) {
        System.out.println(id);

        familyMemberService.deleteFamily(id);
        return success();
    }

    /**
     * 预约列表
     */
    @GetMapping("/reservation/page")
    public AjaxResult page(ReservationPageDto reservationPageDto) {
        startPage();
        List<ReservationPageVo> list = familyMemberService.selectreservationList(reservationPageDto);
        return success(getDataTable(list));
    }

    /**
     * 取消预约
     */
    @PutMapping("/reservation/{id}/cancel")
    public AjaxResult cancel(@PathVariable("id") Long id) {
        familyMemberService.cancelReservation(id);
        return success();
    }
}
